<%--
Copyright (C) 2008 Jackson Harper (jackson@ximian.com)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
--%>

<%@ Control Language="C#" ClassName="GrurrahReadControl" Inherits="Grr.Web.GrurrahControl" %>
<%@ Register Src="AddThisControl.ascx" TagName="AddThisControl" TagPrefix="uc2" %>
<%@ Register Assembly="Gaia.WebWidgets" Namespace="Gaia.WebWidgets" TagPrefix="gaia" %>
<%@ Register Src="RatingControl.ascx" TagName="RatingControl" TagPrefix="uc1" %>

<%@ Import Namespace="Grr.Web" %>
<%@ Import Namespace="Grr.Model" %>

<script runat="server">

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        if (!IsPostBack)
            UpdateGrurrahElements();
    }
    
    
    protected void EditButton_Click(object sender, EventArgs e)
    {
        RaiseBubbleEvent (this, new GrurrahPageModeChangedEventArgs (GrurrahPageMode.Edit));
    }

    public override void UpdateGrurrahElements()
    {
        Grurrah g = (Grurrah)Session["CurrentGrurrah"];
        GrurrahUser user = (GrurrahUser)Session["CurrentUser"];
        if (g != null)
        {
            Title.Text = g.Name;
            DescriptionBody.Text = g.DescriptionBody;

            if (user != null)
            {
                UserGrurrahStatus status = g.GetStatusForUser(user.Name);
                UpdateStatusPanel(status);
                StatusPanel.Visible = true;
            }
        }  
    }

    public override void OnLoginStatusChanged(GrurrahUser user, bool auth)
    {
        if (auth)
        {
            Grurrah g = (Grurrah)Session["CurrentGrurrah"];

            if (g != null)
            {
                UserGrurrahStatus status = g.GetStatusForUser(user.Name);
                UpdateStatusPanel(status);
                StatusPanel.Visible = true;
            }
        } else {
            StatusPanel.Visible = false;
        }
    }

    private void UpdateStatusPanel(UserGrurrahStatus status)
    {
        switch (status)
        {
            case UserGrurrahStatus.DoingIt:
                DoingItLinkButton.Visible = false;
                NotDoingItButton.Visible = true;
                
                WantToDoItLinkButton.Visible = false;
                DontWantToDoItButton.Visible = false;
                break;
            case UserGrurrahStatus.WantToDoIt:
                DoingItLinkButton.Visible = true;
                NotDoingItButton.Visible = false;
                
                WantToDoItLinkButton.Visible = false;
                DontWantToDoItButton.Visible = true;
                break;
            case UserGrurrahStatus.None:
                DoingItLinkButton.Visible = true;
                NotDoingItButton.Visible = false;
                
                WantToDoItLinkButton.Visible = true;
                DontWantToDoItButton.Visible = false;
                break;
        }
    }

    protected void WantToDoItLinkButton_Click(object sender, EventArgs e)
    {
        SetStatus(UserGrurrahStatus.WantToDoIt);
    }
    
    protected void DoingItLinkButton_Click(object sender, EventArgs e)
    {
        SetStatus(UserGrurrahStatus.DoingIt);
    }
    
    private void SetStatus(UserGrurrahStatus status)
    {
        Grurrah g = (Grurrah)Session["CurrentGrurrah"] as Grurrah;
        GrurrahUser u = (GrurrahUser)Session["CurrentUser"] as GrurrahUser;

        if (u != null && g != null) {
            g.SetStatusForUser(u.Name, status);
            UpdateStatusPanel(status);
        }
    }


    protected void NotDoingItButton_Click(object sender, EventArgs e)
    {
        SetStatus(UserGrurrahStatus.None);
    }

    protected void DontWantToDoItButton_Click(object sender, EventArgs e)
    {
        SetStatus(UserGrurrahStatus.None);
    }
</script>


<table border="0" style="margin:15px 15px 15px 15px;">
        <tr>
            <td valign="top">
                <gaia:Label ID="Title" runat="server" style="font-weight: bolder; font-size: x-large;"></gaia:Label>
                <hr />
            </td>
        </tr>
    <tr>
        <td>
            <table>
                <tr>
                    <td>
                        <gaia:Panel ID="StatusPanel" style="margin-bottom:15px;" Visible="false" runat="server">
                            <gaia:LinkButton ID="WantToDoItLinkButton" runat="server" OnClick="WantToDoItLinkButton_Click">[I want to do this]</gaia:LinkButton>
                            <gaia:LinkButton ID="DontWantToDoItButton" runat="server" OnClick="DontWantToDoItButton_Click">[I don't want to do this anymore]</gaia:LinkButton>
                            <gaia:LinkButton ID="DoingItLinkButton" runat="server" OnClick="DoingItLinkButton_Click">[I am doing this]</gaia:LinkButton>
                            <gaia:LinkButton ID="NotDoingItButton" runat="server" OnClick="NotDoingItButton_Click">[I am not doing this anymore]</gaia:LinkButton>
                        </gaia:Panel>
                    </td>
                    <td>&nbsp;</td>
                    <td valign="top">
                        <uc2:AddThisControl ID="AddThisControl1" runat="server" />
                    </td>
                </tr>
            </table>
        </td>
    </tr>
        <tr>
            <td style="vertical-align:top;" valign="top">
                <gaia:Label ID="DescriptionBody" Width="600" runat="server"></gaia:Label>
            </td>
        </tr>
        <tr>
            <td>
                <gaia:LinkButton ID="EditButton" runat="server" OnClick="EditButton_Click" Text="[Edit]" CssClass="ControlLinkButton"></gaia:LinkButton>
            </td>
        </tr>
    </table>